package com.gardeneaters.game_v0.autoplay;

import java.util.Vector;

import com.gardeneaters.game_v0.playingfield.Hexagon;
import com.gardeneaters.game_v0.rules.Hexagons;

class Clusters extends Hexagons{

	private Vector<int[]> clusters; 
	private Vector<int[]> clusterCenters; 
	private int team;
	
	protected Clusters(Hexagon[][] _hexagons, int _team) {
		super(_hexagons);
		this.team = _team;
	}
	
	protected int[] getCenter(int team){
		Medoid medoid = new Medoid(NumOfHexX,NumOfHexY);
		
		for(int i=0; i<NumOfHexY; i=i++)
		{
			for(int j =0; j<NumOfHexX; j++)
			{
				if(hexagons[i][j].getTeam()==team)
					medoid.addHex(j, i);
			}
		}
		
		int [] xy = medoid.getMedoid();
		
		//int[] a = {xy[1], xy[0]};
		//return a;
		return getNearestActiveHexPos(xy[1], xy[0]);
	}
	
	private int[] getNearestActiveHexPos(int i, int j){
		// if teamToFind doesnot exist then there may be errors
		
		Vector <int[]> posVector = new Vector<int[]>();
		int[] tempPos = {i, j};
		posVector.add(tempPos);
		int[] nextPos;
		boolean passedIt = false;
		
		for(int k=0; k<posVector.size(); k++)
		{
			tempPos = posVector.elementAt(k);
			if(hexagons[tempPos[0]][tempPos[1]].getTeam()==team)
				return tempPos;
			
			for(int l =1; l<7; l++)
			{
				
				nextPos = getHexagonPos(l, tempPos[0], tempPos[1]);
				if(nextPos==null)
					continue;
				//System.out.println(" tempPos "+tempPos[0]+" "+tempPos[1]);
				passedIt = VectorPos.isInVector(nextPos[0], nextPos[1], posVector);
				if(!passedIt)
				{
					posVector.add(nextPos);
				}
			}
			//System.out.println(k+"  vector size "+posVector.size());
		}
		
		return null;
	}

	protected Vector<int[]> getCenters() {// there is no nead of this.. enemy class can work with starting points also
		
		for(int k=0; k<clusters.size(); k++)
		{
			Medoid medoid = new Medoid(NumOfHexX,NumOfHexY);
		//	System.out.println("X"+NumOfHexX+" Y"+NumOfHexY);
			int[] tempPos = clusters.elementAt(k);
			traverseGarden(tempPos[0], tempPos[1], medoid);
			int[] xy = medoid.getMedoid();
			tempPos = getNearestActiveHexPos(xy[0], xy[1]);
			clusterCenters.add(tempPos);
		}
		
		return clusterCenters;
		
	}
	
	private void findClusters(){
		
		VectorPos elements = new VectorPos();
		
		for(int i=0; i<NumOfHexY; i++)
		{
			for(int j =0; j<NumOfHexX; j++)
			{
				if(hexagons[i][j].getTeam()==team)
				{
					if(!elements.isInVectorPos(i,j))
					{
						traverseGarden(i,j,elements);
						int[] tempPos = {i, j};
						clusters.add(tempPos);
					}
				}
			}
		}
		//System.out.println("team is "+team+" 9 14 hex team is "+hexagons[9][14].getTeam()+" NumOfHexY "+NumOfHexY);
		
	}
	
	private void traverseGarden(int i, int j, VectorPos elements){

		
		Vector <int[]> posVector = new Vector<int[]>();
		int[] tempPos = {i, j};
		if(hexagons[i][j].getTeam()==team)
		{
			posVector.add(tempPos);
			elements.addPosition(tempPos);
		}
			
		int[] nextPos;
		boolean passedIt = false;
		
		for(int k=0; k<posVector.size(); k++)
		{
			tempPos = posVector.elementAt(k);
			
			for(int l =1; l<7; l++)
			{
				
				nextPos = getHexagonPos(l, tempPos[0], tempPos[1]);
				if(nextPos==null)
					continue;
				passedIt = VectorPos.isInVector(nextPos[0], nextPos[1], posVector);
				if(!passedIt && hexagons[nextPos[0]][nextPos[1]].getTeam()==team)
				{
					posVector.add(nextPos);
					elements.addPosition(nextPos);
				}
			}
		}
		
		
		
	}
	
	private void traverseGarden(int i, int j, Medoid medoid){
		
		Vector <int[]> posVector = new Vector<int[]>();
		int[] tempPos = {i, j};
		if(hexagons[i][j].getTeam()==team)
		{
			posVector.add(tempPos);
			medoid.addHex(tempPos[0], tempPos[1]);
		}
		
		posVector.add(tempPos);
		int[] nextPos;
		boolean passedIt = false;
		
		for(int k=0; k<posVector.size(); k++)
		{
			tempPos = posVector.elementAt(k);
			
			for(int l =1; l<7; l++)
			{
				
				nextPos = getHexagonPos(l, tempPos[0], tempPos[1]);
				if(nextPos==null)
					continue;
				passedIt = VectorPos.isInVector(nextPos[0], nextPos[1], posVector);
				if(!passedIt && hexagons[nextPos[0]][nextPos[1]].getTeam()==team)
				{
					posVector.add(nextPos);
					medoid.addHex(nextPos[0], nextPos[1]);
				}
			}
		}
		
		
		
	}
	
	protected void update() {
		clusters = new Vector<int[]>();
		clusterCenters = new Vector<int[]>();
		findClusters();
		
	}

}
